<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\helpers\VarDumper;
use yii\helpers\ArrayHelper;

use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;

use app\models\MessageModel;

class MessageController extends Controller
{

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }
    public function actionAddmessage()
    {
        $openid = Yii::$app->request->post('openid');
        $userinfo = Yii::$app->request->post('userinfo');
        $image = Yii::$app->request->post('image');
        $title = Yii::$app->request->post('title');
        $content = Yii::$app->request->post('content');
 
        // Yii::$app->db->createCommand()->insert('message',[
        //     'image'=>$image,
        //     'title'=>$title,
        //     'content'=>$content
        //     ])->execute();

        // 新建一条记录
        $model = new MessageModel;
        $model->openid = $openid;
        $model->userinfo = $userinfo;
        $model->image = $image;
        $model->title = $title;
        $model->content = $content;
        
        $model->save();
        
        $id =$model->id;
        $model = MessageModel::find()
            ->where(['id' => $id])
            ->orderBy('id DESC')
            ->asArray()
            ->one();

        $data = array(
            'code' => 0,
            'message' => '新增成功',
            'result' => $model
        );
        $json = json_encode($data);
        return $json;
    }
    public function actionGetmessages()
    {
        $projectid = Yii::$app->request->post('projectid');
        if(empty($projectid)){
        $model = MessageModel::find()
            ->orderBy('id DESC')
            ->asArray()
            ->all();
        }else{
        $model = MessageModel::find()
            ->orderBy('id DESC')
            ->asArray()
            ->all();
        }


        $data = array(
            'code' => 0,
            'message' => '',
            'result' => $model
        );

        $json = json_encode($data);
        return $json;
    }
    public function actionGetmessagebyid()
    {
        $id = Yii::$app->request->post('id');
        $model = MessageModel::find()
            ->where(['id' => $id])
            ->asArray()
            ->one();

        $data = array(
            'code' => 0,
            'message' => '',
            'result' => $model
        );

        $json = json_encode($data);
        return $json;
    }
    public function actionGetmessagebytoday()
    {
        $model = MessageModel::find()
            ->orderBy('id DESC')
            ->asArray()
            ->one();

        $data = array(
            'code' => 0,
            'message' => '',
            'result' => $model
        );

        $json = json_encode($data);
        return $json;
    }    
    public function actionGetmessagesbyopenid()
    {
        $openid = Yii::$app->request->post('openid');
        $model = MessageModel::find()
            ->where(['openid' => $openid])
            ->orderBy('id DESC')
            ->asArray()
            ->all();

        $data = array(
            'code' => 0,
            'message' => '',
            'result' => $model
        );

        $json = json_encode($data);
        return $json;
    }
}
